from django.db import models
from django.contrib.auth import get_user_model
from accounts.models import Laboratory
import uuid

User = get_user_model()


class Project(models.Model):
    """项目模型"""
    
    # 项目状态选择
    STATUS_CHOICES = [
        ('进行中', '进行中'),
        ('已完成', '已完成'),
    ]
    
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name="项目ID")
    name = models.CharField(max_length=200, verbose_name="项目名称")
    description = models.TextField(blank=True, verbose_name="项目描述")
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='进行中', verbose_name="项目状态")
    crop_type = models.CharField(max_length=100, blank=True, verbose_name="作物类型")
    location = models.CharField(max_length=200, blank=True, verbose_name="项目地点")
    start_date = models.CharField(max_length=7, verbose_name="开始日期")  # YYYY-MM 格式
    end_date = models.CharField(max_length=7, verbose_name="结束日期")    # YYYY-MM 格式
    budget = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True, verbose_name="项目预算")
    objectives = models.JSONField(default=list, blank=True, verbose_name="项目目标")
    
    # 关联字段
    manager = models.CharField(max_length=100, blank=True, null=True, verbose_name="项目负责人")
    laboratory = models.ForeignKey(Laboratory, on_delete=models.CASCADE, related_name='projects', verbose_name="所属实验室")
    
    # 时间戳
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    
    class Meta:
        db_table = 'projects_project'
        verbose_name = "项目"
        verbose_name_plural = "项目"
        ordering = ['-created_at']
        
    def __str__(self):
        return f"{self.name} - {self.laboratory.name}"
    
    @property
    def laboratory_name(self):
        """获取实验室名称"""
        return self.laboratory.name if self.laboratory else None
    
    @property
    def manager_name(self):
        """获取负责人姓名"""
        return self.manager if self.manager else None


class ProjectAttachment(models.Model):
    """项目附件模型"""
    
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name="附件ID")
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='attachments', verbose_name="所属项目")
    name = models.CharField(max_length=255, verbose_name="文件名称")
    file = models.FileField(upload_to='project_attachments/%Y/%m/', verbose_name="文件")
    size = models.PositiveIntegerField(verbose_name="文件大小(字节)")
    content_type = models.CharField(max_length=100, verbose_name="文件类型")
    uploaded_by = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="上传者")
    uploaded_at = models.DateTimeField(auto_now_add=True, verbose_name="上传时间")
    
    class Meta:
        db_table = 'projects_attachment'
        verbose_name = "项目附件"
        verbose_name_plural = "项目附件"
        ordering = ['-uploaded_at']
        
    def __str__(self):
        return f"{self.name} - {self.project.name}"
    
    @property
    def url(self):
        """获取文件访问URL"""
        return self.file.url if self.file else None
